home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / LSINSERT.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  161 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lsinsert.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STRING
  11. #include <string.h>
  12. #endif
  13.  
  14. /* library headers */
  15. #include <blkio.h>
  16.  
  17. /* local headers */
  18. #include "lseq_.h"
  19.  
  20. /*man---------------------------------------------------------------------------
  21. NAME
  22.      lsinsert - insert record
  23.  
  24. SYNOPSIS
  25.      #include <lseq.h>
  26.  
  27.      int lsinsert(lsp, buf)
  28.      lseq_t *lsp;
  29.      const void *buf;
  30.  
  31. DESCRIPTION
  32.      The lsinsert function creates a new record following the current
  33.      record and writes the data pointed to by buf into that record.
  34.      The cursor is set to the inserted record.
  35.  
  36.      lsinsert will fail if one or more of the following is true:
  37.  
  38.      [EINVAL]       lsp is not a valid lseq pointer.
  39.      [EINVAL]       buf is the NULL pointer.
  40.      [LSELOCK]      lsp is not write locked.
  41.      [LSENOPEN]     lsp is not open.
  42.  
  43. SEE ALSO
  44.      lsdelcur, lsinsert, lssearch.
  45.  
  46. DIAGNOSTICS
  47.      Upon successful completion, a value of 0 is returned.  Otherwise,
  48.      a value of -1 is returned, and errno set to indicate the error.
  49.  
  50. ------------------------------------------------------------------------------*/
  51. #ifdef AC_PROTO
  52. int lsinsert(lseq_t *lsp, const void *buf)
  53. #else
  54. int lsinsert(lsp, buf)
  55. lseq_t *lsp;
  56. const void *buf;
  57. #endif
  58. {
  59.     int    terrno    = 0;
  60.     bpos_t    bpos    = NIL;
  61.  
  62.     /* validate arguments */
  63.     if (!ls_valid(lsp) || buf == NULL) {
  64.         errno = EINVAL;
  65.         return -1;
  66.     }
  67.  
  68.     /* check if not open */
  69.     if (!(lsp->flags & LSOPEN)) {
  70.         errno = LSENOPEN;
  71.         return -1;
  72.     }
  73.  
  74.     /* check if not write locked */
  75.     if (!(lsp->flags & LSWRLCK)) {
  76.         errno = LSELOCK;
  77.         return -1;
  78.     }
  79.  
  80.     /* build record to insert */
  81.     lsp->clsrp->prev = lsp->clspos;
  82.     if (lsp->clspos == NIL) {
  83.         lsp->clsrp->next = lsp->lshdr.first;
  84.     }
  85.     memcpy(lsp->clsrp->recbuf, buf, lsp->lshdr.recsize);
  86.  
  87.     /* set modify bit in header */
  88.     lsp->lshdr.flags |= LSHMOD;
  89.     if (bputhf(lsp->bp, sizeof(bpos_t),
  90.         (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  91.             sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
  92.         LSEPRINT;
  93.         return -1;
  94.     }
  95.     if (bsync(lsp->bp) == -1) {
  96.         LSEPRINT;
  97.         return -1;
  98.     }
  99.  
  100.     /* get empty block from free list */
  101.     if (bflpop(lsp->bp, &bpos) == -1) {
  102.         LSEPRINT;
  103.         return -1;
  104.     }
  105.     lsp->clspos = bpos;
  106.  
  107.     /* write new record */
  108.     if (ls_rcput(lsp, lsp->clspos, lsp->clsrp) == -1) {
  109.         LSEPRINT;
  110.         terrno = errno;
  111.         bpos = lsp->clspos;
  112.         bflpush(lsp->bp, &bpos);
  113.         errno = terrno;
  114.         return -1;
  115.     }
  116.  
  117.     /* increment record count */
  118.     lsp->lshdr.reccnt++;
  119.  
  120.     /* fix links */
  121.     if (lsp->clsrp->next == NIL) {
  122.         lsp->lshdr.last = lsp->clspos;
  123.     } else {
  124.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->next, offsetof(lsrec_t, prev), &lsp->clspos, sizeof(lsp->clsrp->prev)) == -1) {
  125.             LSEPRINT;
  126.             terrno = errno;
  127.             bpos = lsp->clspos;
  128.             bflpush(lsp->bp, &bpos);
  129.             errno = terrno;
  130.             return -1;
  131.         }
  132.     }
  133.     if (lsp->clsrp->prev == NIL) {
  134.         lsp->lshdr.first = lsp->clspos;
  135.     } else {
  136.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->prev, offsetof(lsrec_t, next), &lsp->clspos, sizeof(lsp->clsrp->next)) == -1) {
  137.             LSEPRINT;
  138.             terrno = errno;
  139.             bpos = lsp->clspos;
  140.             bflpush(lsp->bp, &bpos);
  141.             errno = terrno;
  142.             return -1;
  143.         }
  144.     }
  145.  
  146.     /* clear modify bit in header */
  147.     lsp->lshdr.flags &= ~LSHMOD;
  148.     if (bputhf(lsp->bp, sizeof(bpos_t),
  149.         (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  150.             sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
  151.         LSEPRINT;
  152.         return -1;
  153.     }
  154.     if (bsync(lsp->bp) == -1) {
  155.         LSEPRINT;
  156.         return -1;
  157.     }
  158.  
  159.     return 0;
  160. }
  161.